package org.yxj.customlibrary.v;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;


import org.yxj.customlibrary.R;
import org.yxj.customlibrary.util.Utils;


/**
 * 添加或者修改定制文字
 *
 * @author yxj on 2022/8/2
 */
public class AddCustomTextPop extends FrameLayout {

    private FrameLayout decorView ;

    private View view ;

    private ImageView imCustomBg ;

    private AddCustomTextHeadView mAddCustomTextHeadView;

    private EditText edCustomText ;

    private PainColorListView mPainColorListView;
    private FontListView mFontListView;

    /**
     * 默认颜色为黑色
     */
    private int color = Color.BLACK;


    private Typeface typeface ;

    public AddCustomTextPop(@NonNull Context context) {
        this(context,null);
    }

    public AddCustomTextPop(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AddCustomTextPop(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addView(view = LayoutInflater.from(context).inflate(R.layout.add_custom_text_layout,this,false));
        imCustomBg = view.findViewById(R.id.imCustomBg);
        edCustomText = view.findViewById(R.id.edCustomText);

        mFontListView = view.findViewById(R.id.mFontListView);
        mFontListView.setCheckFontListener(new CheckFont());

        mAddCustomTextHeadView = view.findViewById(R.id.mAddCustomTextHeadView);
        mAddCustomTextHeadView.setAddCustomTextHeadListener(addCustomTextHeadListener);
        mPainColorListView = view.findViewById(R.id.mPainColorListView);
        mPainColorListView.setListener(new PainColorListView.ResultListener() {
            @Override
            public void onResult(int color) {
                edCustomText.setTextColor(color);
                AddCustomTextPop.this.color=color ;
            }
        });
    }


    /**
     * 编辑
     * @param text
     * @return
     */
    public AddCustomTextPop setText(String text){
        edCustomText.setText(text);
        return this ;
    }


    /**
     * 编辑
     * @return
     */
    public AddCustomTextPop setTextColor(int color){
        this.color=color;
        edCustomText.setTextColor(color);
        return this ;
    }

    private AddCustomTextHeadView.AddCustomTextHeadListener addCustomTextHeadListener =   new AddCustomTextHeadView.AddCustomTextHeadListener() {

        @Override
        public void onCancel() {
            dismiss();
        }

        @Override
        public void onFinish() {
            String customText = edCustomText.getText().toString().trim() ;
            if (!TextUtils.isEmpty(customText)){
                if (addCustomTextResultListener!=null){
                    addCustomTextResultListener.onResult(customText,color,typeface);
                }
                dismiss();
            }else {
                Toast.makeText(getContext(),"输出输入内容！！",Toast.LENGTH_SHORT).show();
            }
        }
    };



    public AddCustomTextPop setCustomBg(View view){
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            imCustomBg.setImageBitmap(Utils.blur(getContext(),bitmap,50));
        }
        return this ;
    }


    public AddCustomTextPop setTypeface(Typeface typeface) {
        this.typeface = typeface;
        edCustomText.setTypeface(typeface);
        return this ;
    }



    /**
     * 展示view
     */
    public void show(Activity activity){
        if (activity!=null){
            decorView= (FrameLayout) activity.getWindow().getDecorView();
            decorView.addView(this,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));

            edCustomText.requestFocus() ;
            edCustomText.requestLayout();


            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(edCustomText,0);
        }
    }





    public void dismiss(){
        if (decorView!=null){
            animate().translationY(getMeasuredHeight()).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    decorView.removeView(AddCustomTextPop.this);
                    decorView = null ;
                }
            }).setDuration(300).start();
        }
    }

    private AddCustomTextResultListener addCustomTextResultListener ;

    public AddCustomTextPop setAddCustomTextResultListener(AddCustomTextResultListener addCustomTextResultListener) {
        this.addCustomTextResultListener = addCustomTextResultListener;
        return this ;
    }



    public interface AddCustomTextResultListener{

        /**
         * 定制文字和颜色
         *
         * @param text
         * @param color
         */
        void onResult(String text, int color, Typeface typeface);
    }




    private class CheckFont implements FontListView.CheckFontListener {

        @Override
        public void onCheckFont(Typeface typeface) {
            AddCustomTextPop.this.typeface = typeface;
            if (edCustomText!=null){
                edCustomText.setTypeface(typeface);
            }
        }
    }
}
